home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / Sherlock 2.0 / DevLibSrc / Main_DevLib / LIBfloat.c < prev    next >
Text File  |  1995-11-07  |  3KB  |  109 lines

  1. /*
  2.     devlib: floating point string conversions and es routines.
  3.     
  4.     This file contains all calls to sprintf with floating arguments in DevLib.
  5.  
  6.     Do not use Sherlock in this file.
  7.  
  8.     source:  LIBfloat.c
  9.     started: November 7, 1995.
  10.     version: November 7, 1995.
  11. */
  12.  
  13.  
  14. #include <LIBlib.h>
  15. #include <LIBes.h>
  16.  
  17. #include <stdio.h>        /* For sprintf. */
  18. #include <string.h>        /* For strlen */
  19.  
  20. /*
  21.     Sprintf is just about required for this routine.
  22.     Also, returns 0 unless the floating library is linked in.
  23. */
  24. char *
  25. cvt_double(char * buffer, int buf_size, double d)
  26. {
  27.  
  28.     #if 1 /* correct code. */
  29.     
  30.         int n = sprintf(buffer, "%f", d);
  31.  
  32.     #else /* Use jump table to call Think C to avoid bugs in CC2. */
  33.     
  34.         extern int _THINK_cvt_double(char * buffer, int buf_size, double d);
  35.         int n = _THINK_cvt_double(buffer, buf_size, d);
  36.  
  37.     #endif
  38.  
  39.     #if 0 /* old kludges. */
  40.  
  41.         /*
  42.             applec or TUPLE_C will be defined when compiling CC2 using CC2.
  43.             _lib_cvt_double converts from CC2 to MPW calling conventions for floats.
  44.             (CC2 passes a *pointer* to all floats: MPW passes the float itself.)
  45.     
  46.             Warning: for this kludge to work, *all* calls to sprintf with floating
  47.             arguments must be converted to calls to cvt_double.
  48.     
  49.             The actual conversion takes place as follows:
  50.             In *this* file the prototype of _lib_cvt_double is
  51.     
  52.                 int _lib_cvt_double(char *buffer, double d);
  53.     
  54.             When CC2 compiles the call to _lib_cvt_double it will pass a *pointer* to d.
  55.             Therefore, the prototype for _lib_cvt_double in runlib.c must be
  56.     
  57.                 int _lib_cvt_double(char *buffer, double *d);
  58.     
  59.             since runlib.c is compiled with MPW C, not CC2.
  60.         */
  61.         
  62.         // Warning: Must have %f to format floating constants properly for MPW assembler!
  63.  
  64.         #if defined(THINK_C) || defined(TUPLE_C)
  65.         
  66.             // Kludge: with 8-byte doubles, the library may not match.
  67.             n = sprintf(buffer, "%Lg", (long double) d);
  68.     
  69.         #elif defined(applec)
  70.         
  71.             extern int _lib_cvt_double(char *buffer, double d);
  72.             n = _lib_cvt_double(buffer, d);
  73.     
  74.         #else
  75.         
  76.             #error cvt not defined
  77.     
  78.         #endif
  79.     
  80.     #endif    /* Old Kludges. */
  81.     
  82.     PERM_ASSERT(n < buf_size);
  83.     return buffer;
  84. }
  85.  
  86. void
  87. edouble(double d)
  88. {
  89.     epaddouble(d, 0);
  90. }
  91.  
  92. /*
  93.     Output a double justified in a field of the given length.
  94.     The floating library must be linked for this to work.
  95.     
  96.     Bug fix 1/12/95: some floats can have *very* long defining strings.
  97. */
  98. void
  99. epaddouble(double d, int field_length)
  100. {
  101.     char buf [400];
  102.     size_t n;
  103.  
  104.     /* 7/15/93: replace all calls to sprintf with a floating arg by cvt_double. */
  105.     cvt_double(buf, 400, d);
  106.     n = strlen(buf);
  107.     ejustify(buf, n, field_length);
  108. }
  109.